package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.anno.CustomCache;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.model.album.*;
import com.atguigu.tingshu.vo.album.AlbumCategoryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private AlbumInfoMapper albumInfoMapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;


	@Override
	public List<AlbumCategoryVo> getBaseCategoryList() {
		return baseCategoryViewMapper.getBaseCategoryList();
	}

	@Override
	public List<BaseAttribute> findAttributeByCategoryId(Long category1Id) {
		return albumInfoMapper.findAttributeByCategoryId(category1Id);
	}

	@Override
	@CustomCache(cacheKey = RedisConstant.CACHE_CATEGORY_PREFIX+"#{#params[0]}")
	public BaseCategoryView findBaseCategoryView(Long albumId) {
		return baseCategory3Mapper.findBaseCategoryView(albumId);
	}

	@Override
	public AlbumCategoryVo getBaseCategoryListByC1Id(Long category1Id) {
		return baseCategory3Mapper.getBaseCategoryListByC1Id(category1Id);
	}

	@Override
	public List<BaseCategory3> findTopBaseCategory3ByC1Id(Long category1Id) {
		List<Long> idList = baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>()
						.eq(BaseCategory2::getCategory1Id, category1Id)
						.select(BaseCategory2::getId))
				.stream()
				.map(BaseCategory2::getId)
				.collect(Collectors.toList());
		return baseCategory3Mapper
				.selectList(new LambdaQueryWrapper<BaseCategory3>()
						.in(BaseCategory3::getCategory2Id, idList)
						.eq(BaseCategory3::getIsTop,"1")
						.last("limit 7"));
	}
}
